package com.agentlab.imitation.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.agentlab.imitation.bean.AssociationZyRule;
import com.agentlab.imitation.bean.UserWish;
import com.agentlab.imitation.bean.WishList;
import com.agentlab.imitation.bean.WishListId;
import com.agentlab.imitation.bean.Yx;
import com.agentlab.imitation.bean.Yxzy;
import com.agentlab.imitation.bean.Zy;
import com.agentlab.imitation.bean.Zydl;
import com.agentlab.imitation.common.CommonConstant;
import com.agentlab.imitation.common.CommonMethod;
import com.agentlab.imitation.service.AssociationZyService;
import com.agentlab.imitation.service.DimDydmService;
import com.agentlab.imitation.service.LqkService;
import com.agentlab.imitation.service.UserWishService;
import com.agentlab.imitation.service.WishListService;
import com.agentlab.imitation.service.YxdhyxdmService;
import com.agentlab.imitation.service.YxzydmService;
import com.agentlab.imitation.util.ToString;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unused")
@Controller
public class WishlistAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	WishListService wishListService;
	@Resource
	UserWishService userWishService;
	@Resource
	YxzydmService yxzydmService;
	@Resource
	LqkService lqkService;
	@Resource
	DimDydmService dimDydmService;
	@Resource
	YxdhyxdmService yxdhyxdmService;
	@Resource
	AssociationZyService associationZyService;
	
	
	private UserWish userWish; // 用户偏好
	private WishList wishList;

	public WishList getWishList() {
		return wishList;
	}

	public void setWishList(WishList wishList) {
		this.wishList = wishList;
	}

	private List<WishList> wishs; // 用户志愿表

	public List<WishList> getWishs() {
		return wishs;
	}

	public void setWishs(List<WishList> wishs) {
		this.wishs = wishs;
	}

	public UserWish getUserWish() {
		return userWish;
	}

	public void setUserWish(UserWish userWish) {
		this.userWish = userWish;
	}

	private void loadWishList() {
		List<WishList> wishs = wishListService.getListByUserId(CommonMethod.getUserIdFromSession());
		this.wishs = wishs;
		HttpServletRequest req = ServletActionContext.getRequest();
		req.setAttribute("wishs", wishs);
	}

	/**
	 * 进入志愿单-简洁模式
	 */
	public String simpleCard() {
		loadWishList();
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		userWish = (UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION);
		//测试代码：
		String[] zynames = {"计算机科学与技术","通信工程"};//通信工程 = 现有, 计算机科学与技术 = 现有 -> 电气工程及其自动化 = 现有
		List<AssociationZyRule> rules = associationZyService.getRelatedZy(zynames);
		for(AssociationZyRule rule:rules){
			String results = rule.getRuleString();
			System.out.println(results);
		}
		return "simpleCard";
	}

	/**
	 * 进入志愿单-高级模式
	 */
	public String advanceCard() {
		loadWishList();
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		userWish = (UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION);
		return "advanceCard";
	}

	/**
	 * 重新选择院校
	 */
	public String rechooseyx() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		// String yxdh = (String) session.getAttribute("yxdh");
		String position = (String) session.getAttribute("position");
		cleanSession();
		session.setAttribute("position", position);
		// session.setAttribute("yxdh", yxdh);
		System.out.println("rechooseyx():position=" + position);
		List<Yx> yxList = lqkService.getYxlistOneyear(CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
				Integer.parseInt(userWish.getMode()),dimDydmService.getDymcByDydm(userWish.getArea()));//new ArrayList<Yx>();
//		List<Yx> yxList = new ArrayList<Yx>();
//		yxList.add(new Yx(1, "西南大学2"));
//		yxList.add(new Yx(2, "东南大学2"));
//		yxList.add(new Yx(3, "西北大学2"));
//		yxList.add(new Yx(4, "东北大学2"));
		req.setAttribute("yxList", yxList);
		req.setAttribute("pcname", CommonMethod.getPcnameByposition(position));
		req.setAttribute("yxzyxhmc", CommonMethod.getYxzyxhmc());
		return "yxlist";
	}

	/**
	 * 重新选择专业
	 */
	public String rechoosezy() {
		return addZyForResult();
	}

	private List<Zy> getZylistByYxdh(String yxdh) {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		userWish = (UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION);
		List<Zy> zyList = lqkService.getZyListByYxdm(yxdh, CommonMethod.getPcdm(), this.userWish);//new ArrayList<Zy>();
//		zyList.add(new Zy("1", "外国语言", "1", "re英语"));
//		zyList.add(new Zy("1", "外国语言", "2", "re俄语"));
//		zyList.add(new Zy("1", "外国语言", "3", "西班牙语"));
//		zyList.add(new Zy("1", "外国语言", "4", "日语"));
//		zyList.add(new Zy("1", "外国语言", "5", "德语"));
		return zyList;
	}

	/**
	 * 在院校专业结果页面再增加一个专业
	 */
	public String addZyForResult() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String zyzyxh = req.getParameter("zyzyxh");
		// 专业所在位置
		// String position = (String) session.getAttribute("position");
		// String substr = position.substring(0, position.length()-1);
		// position = substr+zyzyxh;
		// session.setAttribute("position", position);
		req.setAttribute("zyzyxh", zyzyxh);
		// Map<Integer,Zy> map = (Map<Integer,Zy>)
		// session.getAttribute("zydhmap");
		// map.remove(Integer.parseInt(zyzyxh));
		// session.setAttribute("zydhmap", map);
		List<Zy> zyList = getZylistByYxdh((String) session.getAttribute("yxdh"));
		req.setAttribute("zyList", zyList);
		req.setAttribute("pcname", CommonMethod.getPcname());
		req.setAttribute("yxzyxhmc", CommonMethod.getYxzyxhmc());
		return "yxzylist_single2";
	}

	/**
	 * 在院校专业结果页面删除一个专业
	 */
	public String removeZyForResult() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String zyzyxh = req.getParameter("zyzyxh");
		Map<Integer, Zy> map = (Map<Integer, Zy>) session.getAttribute("zydhmap");
		map.remove(Integer.parseInt(zyzyxh));
		session.setAttribute("zydhmap", map);
		return "yxzyresultlist";
	}

	/**
	 * 在院校专业结果页面再增加一个专业-提交
	 */
	public String addZyForResultSubmit() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String zyzyxh = req.getParameter("zyzyxh");
		String zydh = req.getParameter("majorid");
		String zymc = req.getParameter("zymc_" + zydh);
		Map<Integer, Zy> map = (Map<Integer, Zy>) session.getAttribute("zydhmap");
		map.remove(Integer.parseInt(zyzyxh));
		map.put(Integer.parseInt(zyzyxh), new Zy((zydh), zymc));
		session.setAttribute("zydhmap", map);

		return "yxzyresultlist";

	}

	/**
	 * 选择院校
	 */
	public String yx() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String yxdh = req.getParameter("yxdh");
		String position = req.getParameter("position");
		String from = req.getParameter("from");
		session.setAttribute(CommonConstant.ADV_SESSION, from);
		session.setAttribute("position", position);
		session.setAttribute("yxdh", yxdh);
		System.out.println("yx():position=" + position + ",yxdh=" + yxdh);
//		List<Yx> yxList = lqkService.getYxlistOneyear(CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
//				Integer.parseInt(userWish.getMode()),dimDydmService.getDymcByDydm( userWish.getArea()));//new ArrayList<Yx>();
//		List<Yx> yxList2 = lqkService.getYxlistThreeyears(CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
//				Integer.parseInt(userWish.getMode()),dimDydmService.getDymcByDydm( userWish.getArea()));
//		List<Yx> yxList3 = lqkService.getYxlistThreeyears(CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
//				Integer.parseInt(userWish.getMode()),dimDydmService.getDymcByDydm( userWish.getArea()));
		userWish = (UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION);
		List<Yx>[] yxList = lqkService.getYxlistAll(CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
				Integer.parseInt(userWish.getMode()),dimDydmService.getDymcByDydm( userWish.getArea()));//new ArrayList<Yx>();
//		yxList.add(new Yx(1, "西南大学"));
//		yxList.add(new Yx(2, "东南大学"));
//		yxList.add(new Yx(3, "西北大学"));
//		yxList.add(new Yx(4, "东北大学"));
		req.setAttribute("yxList", yxList[0]);
		req.setAttribute("yxList2", yxList[1]);
		req.setAttribute("yxList3", yxList[2]);
		req.setAttribute("pcname", CommonMethod.getPcnameByposition(position));
		req.setAttribute("yxzyxhmc", CommonMethod.getYxzyxhmc());
		return "yxlist";
	}

	/**
	 * 先选择院校后选择专业
	 */
	public String yxzy() {
		// 选择院校后再添加专业,request中取不到position
		// 直接添加专业，request中可取到position

		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String yxdh = req.getParameter("collegeid");
		String yxmc = req.getParameter("collegename");
		session.setAttribute("yxdh", yxdh);
		session.setAttribute("yxmc", yxmc);
		List<Zy> zyList = lqkService.getZyListByYxdm(yxdh,CommonMethod.getPcdm(),(UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION));
//		zyList.add(new Zy("1", "外国语言文学", "1", "英语"));
//		zyList.add(new Zy("1", "外国语言文学", "2", "俄语"));
//		zyList.add(new Zy("1", "外国语言文学", "3", "西班牙语"));
//		zyList.add(new Zy("1", "外国语言文学", "4", "日语"));
//		zyList.add(new Zy("1", "外国语言文学", "5", "德语"));
		
		req.setAttribute("zyList", zyList);
		req.setAttribute("pcname", CommonMethod.getPcname());
		req.setAttribute("klmc", CommonMethod.getKlname());
		req.setAttribute("yxzyxhmc", CommonMethod.getYxzyxhmc());
		return "yxzylist";

	}

	/**
	 * 已有院校，选择一个专业
	 */

	public String yxzySingle() {
		// 选择院校后再添加专业,request中取不到position
		// 直接添加专业，request中可取到position

		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String position = req.getParameter("position");
		String yxdh = req.getParameter("yxdh");
		String yxmc = req.getParameter("yxmc");
		String from = req.getParameter("from");
		if("".equals(yxmc) || yxmc == null){
			yxmc = this.yxdhyxdmService.getYxmcByYxdm(yxdh);
		}
		session.setAttribute("position", position);
		session.setAttribute("yxdh", yxdh);
		session.setAttribute("yxmc", yxmc);
		session.setAttribute(CommonConstant.ADV_SESSION, from);
		// String zydh = req.getParameter("zydh");
		// String zymc = "";
		// session.setAttribute("zydh", zydh);
		// session.setAttribute("zymc", zymc);
		userWish = (UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION);
		List<Zy> zyList = lqkService.getZyListByYxdm(yxdh,CommonMethod.getPcdm(),this.userWish);
//		zyList.add(new Zy("1", "外国语言", "1", "英语"));
//		zyList.add(new Zy("1", "外国语言", "2", "俄语"));
//		zyList.add(new Zy("1", "外国语言", "2", "西班牙语"));
//		zyList.add(new Zy("1", "外国语言", "4", "日语"));
//		zyList.add(new Zy("1", "外国语言", "5", "德语"));
		req.setAttribute("zyList", zyList);
		req.setAttribute("pcname", CommonMethod.getPcnameByposition(position));
		req.setAttribute("klmc", CommonMethod.getKlname());
		req.setAttribute("yxzyxhmc", CommonMethod.getYxzyxhmc());
		System.out.println("yxzy()[直接添加专业（已有院校）]:position=" + position + ",yxdh=" + yxdh);
		return "yxzylist_single";

	}

	/**
	 * 根据院校选择专业
	 */
	public String yxzyresult() {
		HttpServletRequest req = ServletActionContext.getRequest();
		// String position = (String)req.getSession().getAttribute("position");
		// String yxdh = (String)req.getSession().getAttribute("yxdh");

		String[] zydhs = req.getParameterValues("majorid[]");
		String[] zynames = new String[zydhs.length]; //选择的专业名称数组
		if (zydhs != null && zydhs.length > 0) {
			Map<Integer, Zy> map = new HashMap<Integer, Zy>();
			// String[] zyzyxhs = new String[zydhs.length];
			for (int i = 0; i < zydhs.length; i++) {
				Integer zyzyxh = Integer.parseInt(req.getParameter("msort_" + zydhs[i]));
				String zymc = req.getParameter("zymc_" + zydhs[i]);
				map.put(zyzyxh, new Zy((zydhs[i]), zymc));
				zynames[i] = zymc;
			}
			HttpSession session = req.getSession();
			session.setAttribute("zydhmap", map);
		}
		req.setAttribute("pcname", CommonMethod.getPcname());
		req.setAttribute("yxzyxhmc", CommonMethod.getYxzyxhmc());
		//测试代码
//		String[] zynames = {"计算机科学与技术","通信工程"};//通信工程 = 现有, 计算机科学与技术 = 现有 -> 电气工程及其自动化 = 现有
		
		List<AssociationZyRule> rules = associationZyService.getRelatedZy(zynames);
		String relation = "";
		for(AssociationZyRule rule:rules){
			String result = rule.getRuleString();
			relation += result;
		}
		req.setAttribute("relation", relation);

		return "yxzyresultlist";
	}

	/*
	 * public void sortMap(Map<Integer,String> map){
	 * List<Map.Entry<Integer,String>> infoIds = new
	 * ArrayList<Map.Entry<Integer,String>>(map.entrySet());
	 * System.out.println("排序前"); for (int i = 0; i < infoIds.size(); i++) {
	 * String id = infoIds.get(i).toString(); System.out.println(id); }
	 * Collections.sort(infoIds, new Comparator<Map.Entry<Integer,String>>() {
	 * public int compare(Map.Entry<Integer,String> o1,
	 * Map.Entry<Integer,String> o2) { return (o2.getKey() - o1.getKey());
	 * //return (o1.getKey()).toString().compareTo(o2.getKey()); } });
	 * System.out.println("排序前"); for (int i = 0; i < infoIds.size(); i++) {
	 * String id = infoIds.get(i).toString(); System.out.println(id); } }
	 */

	/**
	 * 添加院校专业，先选院校，再选专业
	 */
	public String addyxzy() {
		// for test begin
		// wishList = new WishList();
		// String pc = String.valueOf(Math.abs(new
		// Random(System.currentTimeMillis()).nextInt())%3 + 1);
		// String yxzyxh = String.valueOf(Math.abs(new
		// Random(System.currentTimeMillis()).nextInt())%3 + 1);
		// String zyzyxh = String.valueOf(Math.abs(new
		// Random(System.currentTimeMillis()).nextInt())%3 + 1);
		// WishListId wishListId = new WishListId(1,pc,yxzyxh,zyzyxh);
		// wishList.setId(wishListId);
		// for test end
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String position = (String) session.getAttribute("position");
		String yxdh = (String) session.getAttribute("yxdh");
		String yxmc = (String) session.getAttribute("yxmc");
		Map<Integer, Zy> map = (Map<Integer, Zy>) session.getAttribute("zydhmap"); // 所选专业列表
		// String[] zyzyxhs = (String[]) session.getAttribute("zyzyxhs");

		if (position != null && !"".equals(position) && map != null) {
			String pc = String.valueOf(position.charAt(0));
			String yxzyxh = String.valueOf(position.charAt(1));
			List<Map.Entry<Integer, Zy>> infoIds = new ArrayList<Map.Entry<Integer, Zy>>(map.entrySet());
			wishListService.removeYx(CommonMethod.getUserIdFromSession(), pc, yxzyxh); //先删除当前院校信息
			for (int i = 0; i < infoIds.size(); i++) {
				wishList = new WishList();
				Zy zy = infoIds.get(i).getValue();
				WishListId wishListId = new WishListId(this.userWish.getUserid(), pc, yxzyxh, infoIds.get(i)
						.getKey().toString());
				wishList.setId(wishListId);
				this.wishList.setYxdh(yxdh);
				this.wishList.setYxmc(yxmc);
				this.wishList.setZydh(zy.getZydh().toString());
				this.wishList.setZymc(zy.getZymc());
				System.out.println("WishlistAction.addWish:wishList=" + new ToString(wishList));
				// this.wishListService.delete(this.wishList.getId());
				
				this.wishListService.saveOrUpdate(wishList);
			}
			session.removeAttribute("position");
			session.removeAttribute("yxdh");
			session.removeAttribute("yxmc");
			session.removeAttribute("zydhs");
		} else {
			System.out.println("addWish():position=" + position + ",yxdh=" + yxdh);
		}
		
		String returnStr = "simpleCardRefresh";
		String advSession = (String) session.getAttribute(CommonConstant.ADV_SESSION);
		if(CommonConstant.FROM_ADV.equals(advSession)){
			returnStr="advanceCardRefresh";
		}
		return returnStr;
	}

	/**
	 * 添加专业，已经有院校，添加一个专业
	 */
	public String addZy() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String position = (String) session.getAttribute("position");
		String yxdh = (String) session.getAttribute("yxdh");
		String yxmc = (String) session.getAttribute("yxmc");
		String zydh = req.getParameter("majorid");
		String zymc = req.getParameter("zymc_" + zydh);

		if (position != null && !"".equals(position)) {
			this.wishList = new WishList();
			String pc = String.valueOf(position.charAt(0));
			String yxzyxh = String.valueOf(position.charAt(1));
			String zyzyxh = String.valueOf(position.charAt(2));
			WishListId wishListId = new WishListId(1, pc, yxzyxh, zyzyxh);
			wishList.setId(wishListId);
			wishList.setYxdh(yxdh);
			wishList.setZydh(zydh);
			this.wishList.setYxmc(yxmc);
			this.wishList.setZymc(zymc);
			// this.wishListService.delete(this.wishList.getId());
			this.wishListService.saveOrUpdate(wishList);
		} else {

		}
		cleanSession();
		String returnStr = "simpleCardRefresh";
		String advSession = (String) session.getAttribute(CommonConstant.ADV_SESSION);
		if(CommonConstant.FROM_ADV.equals(advSession)){
			returnStr="advanceCardRefresh";
		}
		return returnStr;
	}

	/**
	 * 专业大类
	 */
	public String zydl() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String from = req.getParameter("from");
		session.setAttribute(CommonConstant.ADV_SESSION, from);
		String position = req.getParameter("position");
		session.setAttribute("position", position); // 从志愿表单过来，将位置存入SESSION
		System.out.println("zydl():position=" + position);

		// List<Zydl> zydlList = new ArrayList<Zydl>();
		// zydlList.add(new Zydl(1, "教育学"));
		// zydlList.add(new Zydl(2, "哲学"));
		// zydlList.add(new Zydl(3, "经济学"));
		// zydlList.add(new Zydl(4, "法学"));
		userWish = (UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION);
		List<Zydl> zydlList = yxzydmService.getZydlList(CommonMethod.getPcdm(),this.userWish.getKl());
		req.setAttribute("zydlList", zydlList);
		req.setAttribute("klmc", CommonMethod.getKlname());
		req.setAttribute("pcname", CommonMethod.getPcnameByposition(position));
		return "zydllist";
	}

	/**
	 * 学科
	 */
	public String yjxklist() {
		HttpServletRequest req = ServletActionContext.getRequest();
		// HttpSession session = req.getSession();
		// String from = req.getParameter("from");
		// session.setAttribute(CommonConstant.ADV_SESSION, from);
		// String position = req.getParameter("position");
		// session.setAttribute("position", position);
		String zydlid = req.getParameter("majortypeid"); // 得到学科门类（专业大类）id
		// List<Zydl> zydlList = new ArrayList<Zydl>();
		// zydlList.add(new Zydl(1, "教育学"));
		// zydlList.add(new Zydl(2, "哲学"));
		// zydlList.add(new Zydl(3, "经济学"));
		// zydlList.add(new Zydl(4, "法学"));
		List<Zydl> yjxklist = yxzydmService.getYjxklist(zydlid);
		req.setAttribute("yjxklist", yjxklist);
		// 找出当前批次显示到页面上
		req.setAttribute("pcname", CommonMethod.getPcname());
		req.setAttribute("klmc", CommonMethod.getKlname());
		return "yjxklist";
	}

	/**
	 * 选择专业
	 */
	public String zy() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String yjxkid = req.getParameter("majortypeid"); // 一级学科代码
		// String zydlName = req.getParameter("zydlname_" + yjxkid);
		List<Zy> zyList = getZylistByYjxkdm(yjxkid);
		req.setAttribute("zyList", zyList);
		req.setAttribute("pcname", CommonMethod.getPcname());
		req.setAttribute("klmc", CommonMethod.getKlname());
		return "zylist";
	}

	private List<Zy> getZylistByYjxkdm(String yjxkdm) {
		List<Zy> zyList = new ArrayList<Zy>();
		// zyList.add(new Zy(zydl.getId(),zydlName,"1",
		// "教育学","111","222","333"));
		// zyList.add(new Zy(zydl.getId(),zydlName,"2", "哲学"));
		// zyList.add(new Zy(zydl.getId(),zydlName,"3", "经济学"));
		// zyList.add(new Zy(zydl.getId(),zydlName,"4", "法学"));
		zyList = yxzydmService.getZyList(yjxkdm);
		return zyList;
	}

	/**
	 * 根据专业选择院校
	 */
	public String zyyx() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String zyid = req.getParameter("mainmajorid");
		String zymc = req.getParameter("majorname_m" + zyid);
		Zy zy = new Zy((zyid), zymc);
		List<Yxzy> yxzyList1 = new ArrayList<Yxzy>();
		List<Yxzy> yxzyList2 = new ArrayList<Yxzy>();
		List<Yxzy> yxzyList3 = new ArrayList<Yxzy>();
		// User user = CommonMethod.getUserFromSession();
		HttpSession session = req.getSession();
		userWish = (UserWish) session.getAttribute(CommonConstant.USER_WISH_SESSION);
//		List<Yx> yxlist1 = lqkService.getYxlistByZyOneYear(zyid, CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
//				Integer.parseInt(userWish.getMode()));
//		List<Yx> yxlist2 = lqkService.getYxlistByZyTwoYears(zyid, CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
//				Integer.parseInt(userWish.getMode()));
//		List<Yx> yxlist3 = lqkService.getYxlistByZyThreeYears(zyid, CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
//				Integer.parseInt(userWish.getMode()));
		List<Yx>[] yxListArray= lqkService.getYxlistByZyAll(zyid, CommonMethod.getPcdm(), userWish.getKl(), CommonMethod.getYxzyxh(), userWish.getPrescore(),
				Integer.parseInt(userWish.getMode()));
		List<Yx> yxlist1 = yxListArray[0];
		List<Yx> yxlist2 = yxListArray[1];
		List<Yx> yxlist3 = yxListArray[2];
		if (yxlist1.size() <= 0 && yxlist2.size() <= 0 && yxlist3.size() <= 0) {
			req.setAttribute("warnmessage", "没有找到适合推荐的院校！");
		} else {
			for (int i = 0; i < yxlist1.size(); i++) {
				Yx yx = (Yx) yxlist1.get(i);
				yxzyList1.add(new Yxzy(yx, zy));
			}
			req.setAttribute("yxzyList1", yxzyList1);
			for (int i = 0; i < yxlist2.size(); i++) {
				Yx yx = (Yx) yxlist2.get(i);
				yxzyList2.add(new Yxzy(yx, zy));
			}
			req.setAttribute("yxzyList2", yxzyList2);
			for (int i = 0; i < yxlist3.size(); i++) {
				Yx yx = (Yx) yxlist3.get(i);
				yxzyList3.add(new Yxzy(yx, zy));
			}
			req.setAttribute("yxzyList3", yxzyList3);
		}
		req.setAttribute("pcname", CommonMethod.getPcname());
		req.setAttribute("klmc", CommonMethod.getKlname());
		req.setAttribute("zymc", zymc);
		return "zyyxlist";
	}

	// private List<Yxzy> getYxByZy(Zy zy) {
	// List<Yxzy> yxzyList = new ArrayList<Yxzy>();
	// yxzyList.add(new Yxzy(new Yx(1,"西南大学"),zy));
	// yxzyList.add(new Yxzy(new Yx(2,"东南大学"),zy));
	// yxzyList.add(new Yxzy(new Yx(3,"西北大学"),zy));
	// yxzyList.add(new Yxzy(new Yx(4,"中南大学"),zy));
	// return yxzyList;
	// }

	/**
	 * 添加专业院校，先选专业再选院校
	 */
	public String addZyyx() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String position = (String) session.getAttribute("position");
		// String yxdh = (String) session.getAttribute("yxdh");

		if (position != null && !"".equals(position)) {
			this.wishList = new WishList();
			String pc = String.valueOf(position.charAt(0));
			String yxzyxh = String.valueOf(position.charAt(1));
			String zyzyxh = String.valueOf(position.charAt(2));
			WishListId wishListId = new WishListId(CommonMethod.getUserIdFromSession(), pc, yxzyxh, zyzyxh);

			String yxdh = req.getParameter("collegeid");
			String yxmc = req.getParameter("collegename_m" + yxdh);
			String zydh = req.getParameter("majorid_m" + yxdh);
			String zymc = req.getParameter("majorname_m" + yxdh);
			zyzyxh = req.getParameter("mjsort");
			wishListId.setZyzyxh(zyzyxh);
			wishList.setId(wishListId);

			this.wishList.setYxdh(yxdh);
			this.wishList.setYxmc(yxmc);
			this.wishList.setZymc(zymc);
			System.out.println("WishlistAction.addWish:wishList=" + new ToString(wishList));
			// this.wishListService.delete(this.wishList.getId());
			this.wishListService.save(wishList);
			session.removeAttribute("position");
			session.removeAttribute("yxdh");
		} else {
			System.out.println("addZyyx():position=" + position);
		}
		String returnStr = "simpleCardRefresh";
		String advSession = (String) session.getAttribute(CommonConstant.ADV_SESSION);
		if(CommonConstant.FROM_ADV.equals(advSession)){
			returnStr="advanceCardRefresh";
		}
		return returnStr;
	}

	public String back() {
		cleanSession();
		String returnStr = "simpleCardRefresh";
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String advSession = (String) session.getAttribute(CommonConstant.ADV_SESSION);
		if(CommonConstant.FROM_ADV.equals(advSession)){
			returnStr="advanceCardRefresh";
		}
		return returnStr;
	}
	/**
	 * 改变志愿次序
	 * @return
	 */
	public String changeSort(){
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		String pc = (String) req.getParameter("pc");
		String action = (String) req.getParameter("action");
		String yxzyxhStr = (String) req.getParameter("yxzyxh");
		if(pc == null || action == null || yxzyxhStr==null){
			return "advanceCardRefresh";
		}
		int yxzyxh1 = Integer.parseInt(yxzyxhStr);
		int yxzyxh2 = yxzyxh1;
		if(action.equals(CommonConstant.SORT_UP)){
			yxzyxh2 = yxzyxh1 - 1;
		}else{
			yxzyxh2 = yxzyxh1 + 1;
		}
		System.out.println("yxzyxh(1)=" + yxzyxh1);
		System.out.println("yxzyxh(2)=" + yxzyxh2);
		List<WishList> wishList1 = wishListService.getListByUserIdPcYxzyxh(CommonMethod.getUserIdFromSession(), pc,String.valueOf(yxzyxh1));
		List<WishList> wishList2 = wishListService.getListByUserIdPcYxzyxh(CommonMethod.getUserIdFromSession(), pc,String.valueOf(yxzyxh2));
		
		for(WishList entity:wishList1){
			wishListService.delete(entity.getId());
		}
		for(WishList entity:wishList2){
			wishListService.delete(entity.getId());
		}
		for(WishList entity:wishList1){
			entity.getId().setYxzyxh(String.valueOf((yxzyxh2)));
			wishListService.save(entity);
		}
		for(WishList entity:wishList2){
			entity.getId().setYxzyxh(String.valueOf((yxzyxh1)));
			wishListService.save(entity);
		}
		return "advanceCardRefresh";
	}

	private void cleanSession() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		session.removeAttribute("position");
		session.removeAttribute("yxdh");
		session.removeAttribute("yxmc");
		session.removeAttribute("zydh");
		session.removeAttribute("zydhmap");
	}
}
